package com.happy3w.game.guess_num;

public class GuessNum {
    private final NumGenerator numGenerator;
    private final NumMatcher numMatcher;

    private int secretNum;
    private int leftTimes = 6;

    public GuessNum(NumGenerator numGenerator, NumMatcher numMatcher) {
        this.numGenerator = numGenerator;
        this.numMatcher = numMatcher;
    }

    public void initGuessNum() {
        secretNum = numGenerator.gen();
    }

    public GuessResult guess(int num) {
        leftTimes--;

        GameStatus status;
        MatchResult matchResult = numMatcher.match(secretNum, num);
        if (matchResult.isSuccess()) {
            status = GameStatus.SUCCESS;
        } else if (leftTimes <= 0) {
            status = GameStatus.FAILED;
        } else {
            status = GameStatus.PROCESSING;
        }
        return new GuessResult(status, matchResult.getEvaluation());
    }
}
